import SECRET from '../../utils/secret'
import CATEGORIES from '../../utils/categories'

Page({
  data: {
    categories: CATEGORIES,
    classify: '',
    list: []
  },
  onLoad: function (options) {
    if (options.classify) {
      this.setData({
        classify: options.classify
      })
    }
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      list: this.data.originalList
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: "",
      list: this.data.originalList
    });
  },
  inputTyping: function (e) {
    let searchData = this.data.originalList.filter(function (item) {
      let reg = new RegExp(e.detail.value, 'i');
      if (reg.test(item.name)) {
        return item
      }
      // if (item.name.indexOf(e.detail.value) !== -1) {
      //   return item;
      // }
    });
    this.setData({
      inputVal: e.detail.value,
      list: searchData
    });
  },
  onShow() {
    this.init()
    this.setData({
      inputShowed: false,
      inputVal: ""
    })
  },
  async init() {
    let { classify } = this.data
    let accountArr = []
    if (classify) {
      accountArr = wx.getStorageSync(CATEGORIES[classify - 1].name)
    } else {
      for (let index = 0; index < CATEGORIES.length; index++) {
        const element = CATEGORIES[index];
        let accountData = wx.getStorageSync(element.name)
        if (accountData) {
          for (let j = 0; j < accountData.length; j++) {
            const element = accountData[j];
            accountArr.push(element)
          }
        }
      }
    }
    this.decryptList(accountArr)
  },
  decryptList(list) {
    if (!Array.isArray(list)) return
    wx.showLoading({ title: '解密中...' })
    for (let i = 0; i < list.length; i++) {
      const item = list[i]
      const { name, account, password, note } = item
      if (name) {
        item.name = SECRET.decrypt(name)
      }
      if (account) {
        item.account = SECRET.decrypt(account)
      }
      if (password) {
        item.password = SECRET.decrypt(password)
      }
      if (note) {
        item.note = SECRET.decrypt(note)
      }
    }
    wx.hideLoading()
    this.setData({
      list: list,
      originalList: list
    })
  },
  onCopy(e) {
    const { data } = e.currentTarget.dataset
    wx.setClipboardData({
      data,
      success: () => {
        wx.showToast({ title: '复制成功' })
      }
    })
  },
  onModify(e) {
    let { account, classify } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/modify/modify?account=${account}&classify=${classify}`,
    })
  },
  onRemove(e) {
    let { account } = e.currentTarget.dataset
    wx.showModal({
      title: '提示',
      content: '是否删除当前账号？',
      success: async ({ confirm }) => {
        if (confirm) {
          let accountArray = this.data.list;
          accountArray.splice(account, 1);
          this.setData({
            list: accountArray
          })
          accountArray = wx.getStorageSync(CATEGORIES[this.data.classify - 1].name)
          accountArray.splice(account, 1);
          wx.setStorageSync(CATEGORIES[this.data.classify - 1].name, accountArray)
          wx.hideLoading()
          wx.showToast({ title: '删除成功', mask: true })
        }
      }
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '账号密码管理助手',
      path: '/pages/index/index',//这里是被分享的人点击进来之后的页面
      imageUrl: '/images/share.jpg'//这里是图片的路径
    }
  }
})